<template>
  <el-dialog
    v-model="visible"
    v-if="load"
    :title="title"
    width="400"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-align="right"
      label-width="120"
      label-position="top"
    >
      <el-form-item label="申报要素名称" prop="project_name">
        <el-input v-model="form.project_name" placeholder="请输入" clearable />
      </el-form-item>

      <el-form-item label="维护格式">
        <el-input v-model="form.project_value" placeholder="请输入" clearable />
      </el-form-item>

      <el-form-item label="备注">
        <el-input
          v-model="form.remark"
          placeholder="请输入"
          type="textarea"
          clearable
          :autosize="{ minRows: 4, maxRows: 6 }"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup lang="jsx">
import { reactive, toRefs } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getCustomsDetail, createDeclareElement, editDeclareElement } from "../../api.js";
const emits = defineEmits(["query"]);
const data = reactive({
  form: {},
  visible: false,
  loading: false,
  formRef: null,
  rules: {
    project_name: [{ required: true, message: "请输入申报要素项名称" }],
  },
  handleType: "",
  title: "",
  load: false,
});
const { rules, form, visible, loading, formRef, handleType, title, load } = toRefs(data);

const statusMap = new Map([
  ["add", "新增"],
  ["edit", "编辑"],
  ["view", "查看"],
]);

const open = async (type, row) => {
  if (!load.value) {
    load.value = true;
    await 1;
  }
  title.value = statusMap.get(type);
  handleType.value = type;
  visible.value = true;
  form.value = Object.assign({}, row);
};

const getDetail = async (id) => {
  try {
    const res = await getCustomsDetail({
      id,
    });
    form.value = res.data;
    if (form.value.declare_elements_list.length == 0) {
      form.value.declare_elements_list.push({
        text: "",
      });
    }
  } catch (e) {
    throw Error(e);
  }
};

const save = () => {
  if (!formRef.value) return;
  formRef.value.validate((valid) => {
    if (valid) {
      const formData = Object.assign({}, form.value);
      loading.value = true;

      if (formData.id) {
        editDeclareElement(formData)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      } else {
        createDeclareElement(formData)
          .then((res) => {
            ElMessage.success(res.message);
            emits("query");
            close();
          })
          .finally(() => {
            loading.value = false;
          });
      }
    }
  });
};

const initForm = () => {
  form.value = getForm();
};
// 初始化
const getForm = () => {
  return {
    project_name: "",
    project_value: "",
    remark: "",
  };
};

onMounted(() => {
  initForm();
});

const close = () => {
  initForm();
  visible.value = false;
};
defineExpose({ open });
</script>
